package cn.acewill.yxd.yunxundian.componet;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;

import cn.acewill.yxd.yunxundian.R;

/**
 * Created by AAA on 2017/4/11.
 */

public class MyProgressBar extends android.widget.ProgressBar {

    private String MprogressText;
    private Paint mPaint;

    public MyProgressBar(Context context) {
        super(context);
        initMyPaint();
    }

    public MyProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initMyPaint();
    }

    public MyProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initMyPaint();
    }

    //获取progress
    @Override
    public synchronized void setProgress(int progress) {
        setText(progress);
        super.setProgress(progress);
    }

    //设置progress位置
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Rect rect=new Rect();
        this.mPaint.getTextBounds(this.MprogressText,0,this.MprogressText.length(),rect);
        int x=(getWidth()/2)-rect.centerX();
        int y=(getHeight()/2)-rect.centerY();
        canvas.drawText(MprogressText,x,y,mPaint);
    }

    //设置画笔
    private void initMyPaint(){
        this.mPaint=new Paint();
        mPaint.setColor(getResources().getColor(R.color.white));
        mPaint.setTextSize(45);

    }

    private void setText(double progress){
        double i=(progress*100)/this.getMax();
        this.MprogressText =String.valueOf(i+"%");
    }
}
